package com.easybuy.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.easybuy.dao.BaseDao;
import com.easybuy.dao.NewsDao;
import com.easybuy.dao.UserDao;
import com.easybuy.entily.News;

public class NewsDaoImpl extends BaseDao implements NewsDao{

	/***
	 * ��ѯ������Ϣ
	 */
	public List<News> AllInfo(int status, int pageNo, int pageSize) {
		Connection conn = getConnection(); // Connection����
		PreparedStatement pstmt = null; // PreparedStatement����
		ResultSet rs = null; // ResultSet����
		List<Object> params = new ArrayList<Object>();
		List<News> list = null; // list����
		News ud = null; // UserDao����
		conn = super.getConnection();
		String sql = "Select * from easybuy_news";
		if (status == 1) {
			sql += "Order by `createTime` limit ?,?";
			params.add((pageNo - 1) * pageSize);
			params.add(pageSize);
		}
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			list = new ArrayList<News>();
			while (rs.next()) {
				ud = new News();
				ud.setId(rs.getInt("id"));
				ud.setTitle(rs.getString("title"));
				ud.setContent(rs.getString("content"));
				ud.setCreateTime(rs.getString("createtime"));
				list.add(ud);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}
	
	/***
	 * ���id��ѯ������Ϣ
	 */
	public News InfoById(int id) {
		Connection conn = null; // Connection����
		PreparedStatement pstmt = null; // PreparedStatement����
		ResultSet rs = null; // ResultSet����
		News ud = null; // UserDao����
		conn = super.getConnection();
		String sql = "select * from easybuy_news where id=?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				ud = new News();
				ud.setId(rs.getInt("id"));
				ud.setTitle(rs.getString("title"));
				ud.setContent(rs.getString("content"));
				ud.setCreateTime(rs.getString("createTime"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			super.closeAll(conn, pstmt, rs);
		}
		return ud;
	}

	@Override
	public List<News> NewsInfo(int id) {//���id������
		// TODO Auto-generated method stub
		Connection conn = getConnection(); // Connection����
		PreparedStatement pstmt = null; // PreparedStatement����
		ResultSet rs = null; // ResultSet����
		List<News> list = null; // list����
		News ud = null; // UserDao����
		conn = super.getConnection();
		String sql = "select * from easybuy_news where id=?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			list = new ArrayList<News>();
			while (rs.next()) {
				ud = new News();
				ud.setId(rs.getInt("id"));
				ud.setTitle(rs.getString("title"));
				ud.setContent(rs.getString("content"));
				ud.setCreateTime(rs.getString("createtime"));
				list.add(ud);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public List<News> AllInfo2() {//查询所有新闻
		// TODO Auto-generated method stub
		Connection conn = getConnection(); // Connection����
		PreparedStatement pstmt = null; // PreparedStatement����
		ResultSet rs = null; // ResultSet����
		List<News> list = null; // list����
		News ud = null; // UserDao����
		conn = super.getConnection();
		String sql = "select * from easybuy_news";
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			list = new ArrayList<News>();
			while (rs.next()) {
				ud = new News();
				ud.setId(rs.getInt("id"));
				ud.setTitle(rs.getString("title"));
				ud.setContent(rs.getString("content"));
				ud.setCreateTime(rs.getString("createtime"));
				list.add(ud);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public int addNews(News news) {//���
		// TODO Auto-generated method stub
		String sql="insert into easybuy_news(title,content,createTime) values(?,?,?)";
		Object [] obj={news.getTitle(),news.getContent(),news.getCreateTime()};
		return super.executeUpdate(sql, obj);
	}

	@Override
	public int updateNewsInfo(News ne) {//�޸�
		// TODO Auto-generated method stub
		News n=new News();
		String sql="UPDATE  easybuy_news SET title=?,content=?,createTime=? WHERE id=?";
		Object []  obj={n.getTitle(),n.getContent(),n.getCreateTime(),n.getId()};
		return super.executeUpdate(sql, obj);
	}

	@Override
	public int delNewsInfo(int id) {//ɾ��
		// TODO Auto-generated method stub
		String sql="DELETE FROM easybuy_news WHERE id=?";
		Object []  obj={id};
		return super.executeUpdate(sql, obj);
	}


}
